package com.zkthink.ceres.store.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zkthink.base.R;
import com.zkthink.base.request.PageParams;
import com.zkthink.base.service.SuperServiceImpl;
import com.zkthink.ceres.common.utils.GaoDeMapUtil;
import com.zkthink.ceres.member.dao.MemberMapper;
import com.zkthink.ceres.store.dao.StoreJoinMapper;
import com.zkthink.ceres.store.dao.StoreMapper;
import com.zkthink.ceres.store.dao.StoreMemberMapper;
import com.zkthink.ceres.store.dto.ApplyStoreDto;
import com.zkthink.ceres.store.dto.ApplyVehiclesDto;
import com.zkthink.ceres.store.dto.ExamineDTO;
import com.zkthink.ceres.store.entity.Store;
import com.zkthink.ceres.store.entity.StoreJoin;
import com.zkthink.ceres.store.entity.StoreMember;
import com.zkthink.ceres.store.service.StoreJoinService;
import com.zkthink.context.BaseContextHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 业务实现类
 *
 * </p>
 *
 * @author chenke
 * @date 2022-04-22
 */
@Slf4j
@Service
public class StoreJoinServiceImpl extends SuperServiceImpl<StoreJoinMapper, StoreJoin> implements StoreJoinService {
    @Autowired
    private StoreMapper storeMapper;
    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private StoreMemberMapper storeMemberMapper;

    /**
     * 申请店铺
     *
     * @param applyStoreDto
     * @return
     */
    @Override
    public R applyStore(ApplyStoreDto applyStoreDto) {
        Long mid = BaseContextHandler.getUserId();
        Store store = storeMapper.selectByMid(mid);
        if (store == null) {
            StoreJoin storeJoin1 = baseMapper.selectByMid(mid);
            if (storeJoin1 == null) {
                StoreJoin storeJoin = new StoreJoin();
                BeanUtils.copyProperties(applyStoreDto, storeJoin);
                JSONObject aMapByLngAndLat = GaoDeMapUtil.getAMapByLngAndLat(applyStoreDto.getLongitude(), applyStoreDto.getLatitude());
                JSONObject addressComponent = aMapByLngAndLat.getJSONObject("addressComponent");
                String registerCity = addressComponent.getString("province") + addressComponent.getString("city") + addressComponent.getString("district");
                storeJoin.setMid(mid);
                storeJoin.setRegisterCity(registerCity);
                storeJoin.setIdImgs(String.join(",", applyStoreDto.getIdImgList()));
                storeJoin.setStoreintroImg(String.join(",", applyStoreDto.getStoreintroImgList()));
                storeJoin.setStorefrontImg(String.join(",", applyStoreDto.getStorefrontImgList()));
                baseMapper.insert(storeJoin);
            } else if (storeJoin1.getApplyType() == 0) {
                return R.fail("审核中");
            }else{
                baseMapper.deleteByMid(mid);
                StoreJoin storeJoin = new StoreJoin();
                BeanUtils.copyProperties(applyStoreDto, storeJoin);
                JSONObject aMapByLngAndLat = GaoDeMapUtil.getAMapByLngAndLat(applyStoreDto.getLongitude(), applyStoreDto.getLatitude());
                JSONObject addressComponent = aMapByLngAndLat.getJSONObject("addressComponent");
                String registerCity = addressComponent.getString("province") + addressComponent.getString("city") + addressComponent.getString("district");
                storeJoin.setMid(mid);
                storeJoin.setRegisterCity(registerCity);
                storeJoin.setIdImgs(String.join(",", applyStoreDto.getIdImgList()));
                storeJoin.setStoreintroImg(String.join(",", applyStoreDto.getStoreintroImgList()));
                storeJoin.setStorefrontImg(String.join(",", applyStoreDto.getStorefrontImgList()));
                baseMapper.insert(storeJoin);
            }
        } else {
            return R.fail("已有实体店 不能在申请！！！");
        }
        return R.success();
    }

    /**
     * 审核
     *
     * @param dto
     * @return
     */
    @Transactional
    @Override
    public R examineStore(ExamineDTO dto) {
        Long userId = BaseContextHandler.getUserId();
        StoreJoin storeJoin = baseMapper.selectById(dto.getId());
        storeJoin.setReason(dto.getReason());
        storeJoin.setApplyUser(userId);
        //审核失败
        if (dto.getApplyType() == 2) {
            storeJoin.setApplyType(2);

        } else if (dto.getApplyType() == 1) {
            storeJoin.setApplyType(1);
            //创建店铺
            Store store = new Store();
            store.setStoreName(storeJoin.getStoreName());
            store.setRemark(storeJoin.getRemark());
            store.setStoreType(storeJoin.getStoreType());
            store.setLongitude(storeJoin.getLongitude());
            store.setLatitude(storeJoin.getLatitude());
            store.setTelephone(storeJoin.getTelephone());
            store.setBusinessHours(storeJoin.getBusinessHours());
            store.setStorefrontImg(storeJoin.getStorefrontImg());
            store.setBackgroundImg(storeJoin.getBackgroundImg());
            store.setTurnover(new BigDecimal(0));
            store.setOrderQuantity(0);
            store.setProvince(storeJoin.getRegisterProvince());
            store.setCity(storeJoin.getRegisterCity());
            store.setArea(storeJoin.getRegisterArea());
            store.setAddress(storeJoin.getAddress());
            store.setMid(storeJoin.getMid());
            store.setMobile(storeJoin.getTelephone());
            store.setContacts(storeJoin.getContacts());
            store.setIdNum(storeJoin.getIdNum());
            store.setIdImgs(storeJoin.getIdImgs());
            store.setCommission(0.0);
            store.setStoreintroImg(storeJoin.getStoreintroImg());
            storeMapper.insert(store);

            StoreMember storeMember = new StoreMember();
            storeMember.setMid(storeJoin.getMid());
            storeMember.setStoreId(store.getId());
            storeMember.setUserType(2);
            storeMemberMapper.insert(storeMember);
            //修改会员身份
            memberMapper.updateUserType(storeJoin.getMid(), storeMember.getUserType());
        }
        baseMapper.updateById(storeJoin);
        return R.success();
    }

    @Override
    public IPage<StoreJoin> IPagelist(PageParams<StoreJoin> page) {
        return baseMapper.IPagelist(page.getPage(), page.getModel());
    }

    @Override
    public int deletStore(Long id) {
        Store store = storeMapper.selectById(id);
        baseMapper.deleteByMid(store.getMid());

        storeMemberMapper.deleteByStoreId(store.getId());

        return storeMapper.deletStore(id);
    }

    @Override
    public R updateStore(ApplyStoreDto applyStoreDto) {
        Store store = storeMapper.selectById(applyStoreDto.getStoreId());
        int i = 0;
        //查询店铺信息
        if (store != null) {
            StoreJoin storeJoin = baseMapper.selectOneByMid(store.getMid());//根据店铺查询审核信息,不包含审核失败的
            BeanUtils.copyProperties(applyStoreDto, store);
            JSONObject aMapByLngAndLat = GaoDeMapUtil.getAMapByLngAndLat(applyStoreDto.getLongitude(), applyStoreDto.getLatitude());
            JSONObject addressComponent = aMapByLngAndLat.getJSONObject("addressComponent");
            String registerCity = addressComponent.getString("province") + addressComponent.getString("city") + addressComponent.getString("district");
            store.setCity(registerCity);
            store.setApplyType(0);
            store.setIdImgs(String.join(",", applyStoreDto.getIdImgList()));
            store.setStoreintroImg(String.join(",", applyStoreDto.getStoreintroImgList()));
            store.setStorefrontImg(String.join(",", applyStoreDto.getStorefrontImgList()));
            i = storeMapper.updateById(store);
            //店铺审核信息修改
            BeanUtils.copyProperties(applyStoreDto, storeJoin);
            storeJoin.setRegisterCity(registerCity);
            storeJoin.setIdImgs(String.join(",", applyStoreDto.getIdImgList()));
            storeJoin.setApplyType(0);
            storeJoin.setStoreintroImg(String.join(",", applyStoreDto.getStoreintroImgList()));
            storeJoin.setStorefrontImg(String.join(",", applyStoreDto.getStorefrontImgList()));
            i = baseMapper.updateById(storeJoin);
        } else {
            //店铺审核信息修改
            StoreJoin storeJoin = baseMapper.selectByMid(BaseContextHandler.getUserId());//根据店铺查询审核信息,不包含审核失败的
            BeanUtils.copyProperties(applyStoreDto, storeJoin);
            JSONObject aMapByLngAndLat = GaoDeMapUtil.getAMapByLngAndLat(applyStoreDto.getLongitude(), applyStoreDto.getLatitude());
            JSONObject addressComponent = aMapByLngAndLat.getJSONObject("addressComponent");
            String registerCity = addressComponent.getString("province") + addressComponent.getString("city") + addressComponent.getString("district");
            storeJoin.setRegisterCity(registerCity);
            storeJoin.setApplyType(0);
            storeJoin.setIdImgs(String.join(",", applyStoreDto.getIdImgList()));
            storeJoin.setStoreintroImg(String.join(",", applyStoreDto.getStoreintroImgList()));
            storeJoin.setStorefrontImg(String.join(",", applyStoreDto.getStorefrontImgList()));
            i = baseMapper.updateById(storeJoin);
        }
        return i > 0 ? R.success() : R.fail("修改失败");
    }
}
